<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>沐硕博客 - 注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">

    <style>
        body {
            background: #f6fbff;
            font-family: 'Segoe UI', sans-serif;
        }

        .register-box {
            max-width: 420px;
            margin: 80px auto;
            background: #ffffff;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0, 123, 255, 0.1);
            padding: 40px 32px;
            animation: fadeInUp 0.6s ease-out;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .register-title {
            color: #3a7bd5;
            text-align: center;
            font-size: 1.75rem;
            font-weight: bold;
            margin-bottom: 24px;
        }

        .form-control:focus {
            box-shadow: 0 0 0 0.2rem rgba(59, 138, 212, 0.25);
            border-color: #3a7bd5;
        }

        .input-group-text {
            background-color: #e9f0ff;
            border-right: none;
            color: #3a7bd5;
        }

        .form-control {
            border-left: none;
        }

        .btn-primary {
            background-color: #3a7bd5;
            border-color: #3a7bd5;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #2c65b5;
        }

        .login-link {
            text-align: right;
            margin-top: 12px;
        }

        .login-link a {
            color: #6ec1e4;
            text-decoration: none;
            font-size: 0.9rem;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .alert {
            font-size: 0.85rem;
        }

        .spinner-border-sm {
            width: 1rem;
            height: 1rem;
        }
    </style>
</head>
<body>

<div class="register-box">
    <!-- 品牌 Logo -->
    <div class="text-center mb-4">
        <i class="bi bi-chat-left-dots display-4 text-primary"></i>
        <h4 class="mt-2">创建账户</h4>
    </div>

    <!-- 注册表单 -->
    <form th:action="@{/api/auth/register}" method="post">
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-person"></i></span>
            <input type="text" name="username" class="form-control" required placeholder="请输入用户名" autofocus onfocus="this.scrollIntoView({ behavior: 'smooth' });">
        </div>

        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-envelope"></i></span>
            <input type="email" name="email" class="form-control" placeholder="可用于找回密码">
        </div>

        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-lock"></i></span>
            <input type="password" name="password" class="form-control" required placeholder="请输入密码">
        </div>

        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-check2-circle"></i></span>
            <input type="password" name="confirmPassword" class="form-control" required placeholder="请再次输入密码">
        </div>

<!--        <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">-->
        <button type="submit" class="btn btn-primary w-100" onclick="this.classList.add('disabled'); this.innerHTML = '<span class=\'spinner-border spinner-border-sm me-2\' role=\'status\' aria-hidden=\'true\'></span>正在注册';">
            注册
        </button>
    </form>

    <!-- 成功/错误提示（可由后端传参） -->
    <div th:if="${param.success}" class="alert alert-success mt-3 text-center d-flex align-items-center justify-content-center">
        <i class="bi bi-check-circle-fill me-2"></i>注册成功！<a th:href="@{/login}">去登录</a>
    </div>

    <div th:if="${param.error}" class="alert alert-danger mt-3 text-center d-flex align-items-center justify-content-center">
        <i class="bi bi-exclamation-triangle-fill me-2"></i>注册失败：用户名已存在
    </div>

    <!-- 新增通用错误提示 -->
    <div th:if="${param.dbError}" class="alert alert-warning mt-3 text-center">
        <i class="bi bi-database me-2"></i>数据库连接异常，请检查服务器日志
    </div>

    <!-- 添加自动跳转的脚本 -->
    <script th:if="${param.success}">
        // 显示提示对话框
        alert('注册成功！即将跳转到登录页面。');
        // 跳转到登录页面
        window.location.href = '/login';
    </script>

    <!-- 登录链接 -->
    <div class="login-link">
        已有账号？<a th:href="@{/login}">去登录</a>
    </div>
</div>

<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
